package work.comeback.omo.controller.servlet.movie;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static org.mockito.Mockito.*;

public class MovieInfoServletTest {

    private MovieInfoServlet servlet;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private StringWriter responseWriter;

    @BeforeEach
    public void setUp() throws IOException {
        servlet = new MovieInfoServlet();
        request = mock(HttpServletRequest.class);
        response = mock(HttpServletResponse.class);
        responseWriter = new StringWriter();
        when(response.getWriter()).thenReturn(new PrintWriter(responseWriter));
    }

    @Test
    public void testGetRequest() throws ServletException, IOException {
        when(request.getMethod()).thenReturn("GET");
        when(request.getParameter("query")).thenReturn("action");

        servlet.service(request, response);

        verify(response).setContentType("application/json");
        verify(response).setCharacterEncoding("UTF-8");
        // 检查响应内容
        System.out.println(responseWriter.toString());
    }

    @Test
    public void testPutRequest() throws ServletException, IOException {
        when(request.getMethod()).thenReturn("PUT");
        when(request.getParameter("vodId")).thenReturn("1");
        when(request.getParameter("vodName")).thenReturn("New Movie Name");

        servlet.service(request, response);

        verify(response).setContentType("application/json");
        verify(response).setCharacterEncoding("UTF-8");
        // 检查响应内容
        System.out.println(responseWriter.toString());
    }

    @Test
    public void testDeleteRequest() throws ServletException, IOException {
        when(request.getMethod()).thenReturn("DELETE");
        when(request.getParameter("vodId")).thenReturn("1");

        servlet.service(request, response);

        verify(response).setContentType("application/json");
        verify(response).setCharacterEncoding("UTF-8");
        // 检查响应内容
        System.out.println(responseWriter.toString());
    }
}